<?php

 class __kernel_start {
 
 	static $classsname;
    static private $instances;
        
    static function si($class, $type = 'module', $check = true) {
    	self::onlyStartModule($class, $type, $check);

        return self::$instances["__{$type}_{$class}"];
    }

    static function onlyStartModule($classname, $type = 'module', $check=true) {
 		
 		if($type=="module" && $check == true) {
	 		if(self::checkEnabled($classname, $type) == false) {
	 		    $classname_ex = explode('_', $classname);
	 		    if(!isset($classname_ex[1]) || self::checkEnabled($classname_ex[0]) == false)
	 		        return false;
	 		}
 		} 
 		
 		$classname = "__{$type}_{$classname}";

 		if(!self::$instances[$classname]) {
			self::$instances[$classname] = new $classname;
 		}
			
		return true;
 	}
 	 	
 	static function kernel($classname, $page="", $title="", $args=array()) {
		
		$classname = "__kernel_".$classname;
		$return    =  &__kernel_start::start($classname, $page, $args);
		
		if(!self::$instances[$classname]->title)
			self::$instances[$classname] -> title = $title;
		
		return $return;

	}
	
 	static function module($classname, $page="", $title="", $args=array(), $secret=false) {
		
 		if($secret == false)
 			if(!self::checkEnabled($classname)) return false;
 		
 		global $init;
		
		$classname = "__module_".$classname;
		$return    =  &__kernel_start::start($classname, $page, $args);
		
		if(!self::$instances[$classname]->title)
			self::$instances[$classname] -> title = $title;
		
		return $return;

	}
	
 	static function block($classname, $page="", $args=array()) {

		$classname = "__module_".$classname;
		$return    =  &__kernel_start::start($classname, "block_".$page, $args);
		return $return;

	}
	
	static private function start($classname, $page, $argsToModule) {
		
		if(!self::$instances[$classname])
			self::$instances[$classname] = new $classname;
		
		$obj  = &self::$instances[$classname];
		$urls = &$obj->urls;
		
		if(is_array($urls[$page])) {
			if(!isset($urls[$page][1]) || $urls[$page][1] === false) {
				$sub_page = $page;
			} else {
				$sub_page = (isset($_GET[$urls[$page][1]])?$_GET[$urls[$page][1]]:$page);
			}
			return self::start('__module_'.$urls[$page][0], $sub_page, $urls[$page][2]);
		}
		
		if(empty($page) || $page == 'default') {
			$page = $urls['default'];
		}
		elseif(!isset($urls[$page]) && isset($urls['*'])) $page = '*';
		
		if(empty($urls[$page])) {
			return __kernel_error::e404(false, 'page_notFound');
		}
		
		// Проверим карту доступа - свойство $acsMap, в котором, возможно запретили доступ к этой странице
		if(is_array($obj->acsMap) && is_array($obj->acsMap[$page])) {
			$acs_access = &$obj->acsMap[$page][0];
			$acs_module = &$obj->acsMap[$page][1];
			
			if(!drv(ACSGP)->doIHaveAccess($acs_access, $acs_module)) return __kernel_error::e404(false, 'access');
			
		}
		
		return $obj->$urls[$page]($argsToModule);
	}
	
	
	/* Check. Enable this module or not */
	function checkEnabled($module, $type = 'module') {
		if($type == 'module') {
			if(is_array(__kernel_init::$modules[$module])) {
				return true;
			} else { return false; }
		} else {
			return true;
		}
	}
 
 }

?>
